package com.eggplant.timetools;

import uij.wf.kf.pouzzt.R;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

import com.eggplant.timetools.utils.MyLog;
import com.eggplant.timetools.utils.PreferencesData;
import com.eggplant.timetools.view.AlarmPreference;
import com.eggplant.timetools.view.MyTitleView;
import com.umeng.analytics.MobclickAgent;

public class SettingEggActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
	public static final String KEY_DEFAULT_RINGTONE_INTERV = "default_ringtone0";
	public static final String KEY_DEFAULT_RINGTONE = "default_ringtone";
	static final String KEY_DEFAULT_ENDTIME = "endtime_notice_list";
	static final String KEY_DEFAULT_VOICE = "alarm_in_voice_mode";
	ListPreference listPref_end;
    CheckBoxPreference cbp;
    AlarmPreference interv_ringtone;

	private MyTitleView myTitleView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_Translucent_NoTitleBar);
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);	
		initRingtone();
		
	}

	

	private void initRingtone() {
		final AlarmPreference end_ringtone = (AlarmPreference) findPreference(KEY_DEFAULT_RINGTONE);
		 interv_ringtone = (AlarmPreference) findPreference(KEY_DEFAULT_RINGTONE_INTERV);
		String str_uri1 = PreferencesData.getEndVoiceUri(getApplicationContext());
		if (str_uri1 != "") {
			end_ringtone.setAlert(Uri.parse(str_uri1));
		} else {
			Uri alert = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
			if (alert != null) {
				end_ringtone.setAlert(alert);
			}
		}
		end_ringtone.setChangeDefault();

		String str_uri0 = PreferencesData.getIntervVoiceUri(getApplicationContext());
		if (str_uri0 != "") {
			interv_ringtone.setAlert(Uri.parse(str_uri0));
		} else {
			Uri alert = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
			if (alert != null) {
				interv_ringtone.setAlert(alert);
			}
		}
		interv_ringtone.setChangeDefault();

		listPref_end = (ListPreference) findPreference(KEY_DEFAULT_ENDTIME);
		// listPref_end.setSummary(listPref_end.getEntry());
		listPref_end.setOnPreferenceChangeListener(this);
	
		cbp=(CheckBoxPreference) findPreference(KEY_DEFAULT_VOICE);
		cbp.setOnPreferenceChangeListener(this);
		boolean checked =PreferencesData.getDefaultVoice(getApplicationContext());
		cbp.setChecked(checked);
		interv_ringtone.setEnabled(!checked);
		
	}

	@Override
	protected void onResume() {
		refresh();
		MobclickAgent.onResume(this);
		super.onResume();
	}
	

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	private void refresh() {
		listPref_end.setSummary(listPref_end.getEntry());

	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		if (KEY_DEFAULT_ENDTIME.equals(preference.getKey())) {
			ListPreference listPref_end = (ListPreference) preference;
			final int idx = listPref_end.findIndexOfValue((String) newValue);
			listPref_end.setSummary(listPref_end.getEntries()[idx]);
			MyLog.i("onPrefenceChange value==" + (String) newValue);
			if (KEY_DEFAULT_ENDTIME.equals(preference.getKey())) {
				PreferencesData.setEndTime(getApplicationContext(), Integer.parseInt((String) newValue));
			}
			return true;
		}else if(KEY_DEFAULT_VOICE.equals(preference.getKey())){
			boolean check=(Boolean) newValue;
			interv_ringtone.setEnabled(!check);
			PreferencesData.setDefaultVoice(getApplicationContext(), check);
			return true;
		}
		return false;
	}

}
